/**
 * Created by lwy on 2017/5/27.
 */

var phone = getItem('phone');
var verifyCode;

//发送验证码
$('.code-button').click(function(){
    sendCode();
})
function sendCode(){

    if(!ifCyber()){
        mui.toast('请检查网络重试');
    }
    if(!ifCyber()){
        mui.toast('请检查网络');
    }

    verify();


}

function verify() {
    $(".code-button").html("发送中…");
    $(".get-code").unbind();
    $.ajax({
        url:'/user/sendMobileCode_Vtoken.json',
        data:{
            platform:'wap',
            token:getItem('token'),

        },
        dataType:'json',
        success:function(data) {
            if(data.code!=1){
                mui.toast(data.msg);
                $(".code-button").html("重新发送");
                $(".get-code").bind('click',sendCode);
                return;
            }
            timeUpdate.init($(".code-button"), 60);

        }
    })
}

var timeUpdate = {
    node:null,//节点
    time:60,//时间
    //主函数
    start:function(){
        if(this.time > 0){
            $(this.node).html( this.time-- + 's后重发');
            var _this = this;
            setTimeout(function(){
                _this.start();
            },1000);
        }else{
            this.node.click(function () {
                sendCode();
            });
            this.node.html("重新发送");
        }
    },
    //初始化
    init:function(node, timeLimit){
        this.node = node;
        this.time = timeLimit;
        this.start();
    }
};


//确定
$('.set-certain').click(function(){
    var userName = phone;
    var passWord = $('#password').val();
    var conpassword = $('#conpassword').val();
    if(passWord==""||conpassword==""){
        mui.toast('密码不能为空');
        return;
    }
    if(passWord!=conpassword){
        mui.toast('两次输入的密码不一样，请重新输入');
        return;
    }
    if(!ifCyber()){
        mui.toast('请检查网络重试');
    }
                $.ajax({
                    url:'/user/userLoginKey.json',
                    dataType:'json',
                    data:{
                        name:userName,
                    },
                    success:function(data){
                        if(data.code!=1){
                            mui.toast(data.msg);
                            return;
                        }
                        if(data.code==1){
                            passWord= calcMD5(passWord + data.datas.userKey, 32);
                            $.ajax({
                                url:'/user/updatePassword.json',
                                dataType:'json',
                                type:'post',
                                data:{
                                    token:getItem('token'),
                                    payPassword:passWord,
                                    mobile_code:$('.code-input').val(),
                                    platform:'wap',
                                },
                                success:function(data){
                                    if(data.code!=1){
                                        mui.toast(data.msg);
                                        return;
                                    }
                                    if(data.code==1){
                                        mui.alert('修改成功',function(){
                                            window.location.href="set.html";
                                        })
                                    }
                                }
                            })
                        }
                    }
                })
})